package com.complaintcase.system.service.impl;

import java.util.List;

import com.complaintcase.common.exception.ServiceException;
import com.complaintcase.common.utils.SecurityUtils;
import com.complaintcase.common.utils.StringUtils;
import com.complaintcase.system.domain.CaseInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.complaintcase.system.mapper.CaseBestInfoMapper;
import com.complaintcase.system.domain.CaseBestInfo;
import com.complaintcase.system.service.ICaseBestInfoService;

/**
 * 优秀录音案例库Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-28
 */
@Service
public class CaseBestInfoServiceImpl implements ICaseBestInfoService 
{
    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Autowired
    private CaseBestInfoMapper caseBestInfoMapper;

    /**
     * 查询优秀录音案例库
     * 
     * @param id 优秀录音案例库主键
     * @return 优秀录音案例库
     */
    @Override
    public CaseBestInfo selectCaseBestInfoById(Long id)
    {
        return caseBestInfoMapper.selectCaseBestInfoById(id);
    }

    /**
     * 查询优秀录音案例库列表
     * 
     * @param caseBestInfo 优秀录音案例库
     * @return 优秀录音案例库
     */
    @Override
    public List<CaseBestInfo> selectCaseBestInfoList(CaseBestInfo caseBestInfo)
    {
        return caseBestInfoMapper.selectCaseBestInfoList(caseBestInfo);
    }

    /**
     * 新增优秀录音案例库
     *
     * @param caseBestInfo 优秀录音案例库
     * @return 结果
     */
    @Override
    public int insertCaseBestInfo(CaseBestInfo caseBestInfo)
    {
        Long userId = SecurityUtils.getUserId();
        caseBestInfo.setUserId(userId);
        return caseBestInfoMapper.insertCaseBestInfo(caseBestInfo);
    }

    @Override
    public String importCaseInfo(List<CaseBestInfo> caseInfoList, Long userId) {
        if (StringUtils.isNull(caseInfoList) || caseInfoList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (CaseBestInfo caseInfo : caseInfoList)
        {
            try
            {
                // 验证是否存在这个用户
                caseBestInfoMapper.insertCaseBestInfo(caseInfo);
                successNum++;
                successMsg.append("<br/>" + successNum + "、工单编号 " + caseInfo.getCaseNo() + " 导入成功");

            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、工单编号 " + caseInfo.getCaseNo() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 修改优秀录音案例库
     * 
     * @param caseBestInfo 优秀录音案例库
     * @return 结果
     */
    @Override
    public int updateCaseBestInfo(CaseBestInfo caseBestInfo)
    {
        return caseBestInfoMapper.updateCaseBestInfo(caseBestInfo);
    }

    /**
     * 批量删除优秀录音案例库
     * 
     * @param ids 需要删除的优秀录音案例库主键
     * @return 结果
     */
    @Override
    public int deleteCaseBestInfoByIds(Long[] ids)
    {
        return caseBestInfoMapper.deleteCaseBestInfoByIds(ids);
    }

    /**
     * 删除优秀录音案例库信息
     * 
     * @param id 优秀录音案例库主键
     * @return 结果
     */
    @Override
    public int deleteCaseBestInfoById(Long id)
    {
        return caseBestInfoMapper.deleteCaseBestInfoById(id);
    }
}
